package it.ma.musicplayernew.loaders;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.zhongdao.swing.R;

import java.io.File;
import java.util.List;

import it.ma.musicplayernew.helper.ImageLoaderHelper;
import it.ma.musicplayernew.modle.Track;
import it.ma.musicplayernew.views.ViewHolderList;

/**
 * @author Nick
 * 
 */
public class TrackListAdapter extends BaseAdapter {

    private List<Track> mTracks;
    private long currTackId;
    private Context mContext;
    private ViewHolderList mViewHolderList;
    static String mArtworkUri = "content://media/external/audio/albumart";
    private boolean isListScrolling = false;

    public TrackListAdapter(Context mContext) {
        super();
        this.mContext = mContext;
    }

    public TrackListAdapter(List<Track> mTracks, Context mContext) {
        super();
        this.mTracks = mTracks;
        this.mContext = mContext;
    }

    /**
     * @param scrolling
     */
    public void isListScrolling(boolean scrolling) {
        this.isListScrolling = scrolling;
        if (!scrolling)
            notifyDataSetChanged();
    }

    public void updateList(List<Track> tracks) {
    	this.mTracks = tracks;
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.layout_list_item, null);
            mViewHolderList = new ViewHolderList(convertView);
            convertView.setTag(mViewHolderList);
        } else {
            mViewHolderList = (ViewHolderList) convertView.getTag();
        }
        // update list content
        Track track = mTracks.get(position);
        mViewHolderList.mTitleView.setText(track.getTitle());
        mViewHolderList.mArtistView.setText(track.getArtist());
        mViewHolderList.mIndicatorView.setVisibility(track.getId() == currTackId ? View.VISIBLE : View.INVISIBLE);
        convertView.setBackgroundColor(track.getId() == currTackId ? mContext.getResources().getColor(R.color.holo_gray_bright)
                : mContext.getResources().getColor(R.color.transparent));
        // art
        String uri = mArtworkUri + File.separator + track.getAlbumId();

        if (!isListScrolling)
            ImageLoaderHelper.displayImage(uri, mViewHolderList.mArtView, R.mipmap.default_artist);

        return convertView;
    }

    @Override
    public int getCount() {
        return mTracks.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    public List<Track> getTracks() {
        return mTracks;
    }

    public long getCurrTackId() {
        return currTackId;
    }

    public void setCurrTackId(long currTackId) {
        this.currTackId = currTackId;
    }


    public void setTracks(List<Track> mTracks) {
        this.mTracks = mTracks;
        notifyDataSetChanged();
    }
    /**
     * @author Nick
     * 
     *//*
    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                FadeInBitmapDisplayer.animate(imageView, 1000);
                if (firstDisplay) {
                    displayedImages.add(imageUri);
                }
            }
        }
    }*/

}
